//
//  ViewController.m
//  FlutterTest
//
//  Created by zxw on 2021/4/2.
//

#import "ViewController.h"
#import <Flutter/Flutter.h>
#import "AppDelegate.h"

@interface ViewController ()<FlutterStreamHandler>
@property (nonatomic, copy) FlutterEventSink eventSink;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
}

- (IBAction)btnClick:(id)sender {
    
    FlutterViewController *flutterViewController = [[FlutterViewController alloc]initWithProject:nil initialRoute:@"home?{\"message\":\"嗨，本文案来自第一个原生页面，将在Flutter页面看到我\"}" nibName:nil bundle:nil];
    [self.navigationController pushViewController:flutterViewController animated:YES];

}

- (IBAction)methodChannel:(id)sender {
    
    FlutterViewController *flutterViewController = [[FlutterViewController alloc]initWithProject:nil initialRoute:@"home" nibName:nil bundle:nil];
    [self.navigationController pushViewController:flutterViewController animated:YES];
    
    // 要与main.dart中一致
    NSString *channelName = @"channel_home";

    FlutterMethodChannel *messageChannel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:flutterViewController.binaryMessenger];

    [messageChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        if ([call.method isEqualToString:@"initWithPrama"]) {
            NSLog(@"flutter传给原生的参数：%@",call.arguments);
            if (result) {
                result(@"原生返回给flutter的内容");
            }
        }
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [messageChannel invokeMethod:@"nativeCallFlutter" arguments:@"原生传给Flutter的参数" result:^(id  _Nullable result) {
            NSLog(@"%@",result);
        }];
    });
}
- (IBAction)eventChannel:(id)sender {
    
    FlutterViewController *flutterViewController = [[FlutterViewController alloc]initWithProject:nil initialRoute:@"detail" nibName:nil bundle:nil];
    [self.navigationController pushViewController:flutterViewController animated:YES];
    // 要与detail.dart中一致
    NSString *channelName = @"channel_detail";
    FlutterEventChannel *evenChannal = [FlutterEventChannel eventChannelWithName:channelName binaryMessenger:flutterViewController.binaryMessenger];
    // 代理FlutterStreamHandler
    [evenChannal setStreamHandler:self];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.eventSink(@"多次传递：iOS传给Flutter的参数");
    });
}
- (IBAction)messageChannel:(id)sender {
    FlutterViewController *flutterViewController = [[FlutterViewController alloc]initWithProject:nil initialRoute:@"message" nibName:nil bundle:nil];
    [self.navigationController pushViewController:flutterViewController animated:YES];
    NSString *channelName = @"channel_message";
    FlutterBasicMessageChannel *messageChannel = [FlutterBasicMessageChannel messageChannelWithName:channelName binaryMessenger:flutterViewController.binaryMessenger];
    // 接收消息监听
    [messageChannel setMessageHandler:^(id message, FlutterReply callback) {
        
        NSLog(@"iOS收到flutter发送的消息 %@",message);
        callback(@"iOS接收到Flutter消息后回调");
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [messageChannel sendMessage:@"iOS主动发送消息给Flutter"];
    });
}


#pragma mark - FlutterStreamHandler
- (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
                                       eventSink:(FlutterEventSink)eventSink{
    //这里可以保存这个对象 后面可以再次调用
    if (eventSink) {
        self.eventSink = eventSink;
    }
    
    if ([arguments isKindOfClass:NSString.class]) {
        NSString *event = arguments;
        if ([event isEqualToString:@"init"]) {
            if (eventSink) {
                eventSink(@"iOS传给Flutter的参数");
            }
        }
    }
    return nil;
}
 
- (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments {
    return nil;
}

@end
